const express = require("express");
const router = express.Router();
const jwt = require("jsonwebtoken");

const userDao = require("../database/UserDao");
const cfg = require("../config/config");

//登录
router.post("/login",async(req,res) => {
    let {account,pwd} = req.body;
    let user = await userDao.loginUser(account,pwd);
    if(user) {
        user.user_pwd = null;
        //制作jwt凭证（把用户信息进行特殊处理，有验签,验签可理解为封条）
        let jwttoken = jwt.sign(user,cfg.jwtSecret,{expiresIn:'1h'})
        // console.log(jwttoken);
        let r = {code:2000,msg:'登录成功',data:{user,jwttoken}};
        res.send(r);
    }else{
        res.send({code:4001,msg:'登录失败',data:user});
    }
})

//修改密码
router.post("/modify",async(req,res) => {
    let {account,oldPwd,newPwd} = req.body;
    let r = await userDao.updateUserPwd(account,oldPwd,newPwd);
    if(r) {
        res.send({code:2000,msg:'修改密码成功',data:r});
    }else{
        res.send({code:4001,msg:'修改密码失败',data:r});
    }
})
module.exports = router;